﻿Imports System.IO
Imports System.Text
Imports System.Diagnostics
Imports System.Security.Cryptography

Namespace Hash

    Public Class RIPEMD160

        Public Function Checksum(ByVal Arquivo As String) As String
            Dim md5 As System.Security.Cryptography.RIPEMD160Managed = New System.Security.Cryptography.RIPEMD160Managed
            Dim fs As FileStream = New FileStream(Arquivo, FileMode.Open, _
            FileAccess.Read, FileShare.Read, 8192)
            fs = New FileStream(Arquivo, FileMode.Open, FileAccess.Read, FileShare.Read, 8192)
            md5.ComputeHash(fs)
            fs.Close()
            Dim hash As Byte() = md5.Hash
            Dim sb As StringBuilder = New StringBuilder
            Dim hByte As Byte
            For Each hByte In hash
                sb.Append(String.Format("{0:X2}", hByte))
            Next
            Return sb.ToString()
        End Function

        Public Function GeraString(ByVal Texto As String, Optional ByVal Hexa As Boolean = True) As String
            Dim btyScr() As Byte = ASCIIEncoding.ASCII.GetBytes(Texto)
            Dim objMd5 As New System.Security.Cryptography.RIPEMD160Managed
            Dim btyRes() As Byte = objMd5.ComputeHash(btyScr)

            If Not Hexa Then
                Return Convert.ToBase64String(btyRes)
            Else
                Dim intTotal As Integer = (CInt(btyRes.Length * 2) + CInt((btyRes.Length / 8)))
                Dim strRes As StringBuilder = New StringBuilder(intTotal)
                Dim intI As Integer

                For intI = 0 To btyRes.Length - 1
                    strRes.Append(BitConverter.ToString(btyRes, intI, 1))
                Next intI

                Return strRes.ToString().TrimEnd(New Char() {" "c}).ToLower
            End If
        End Function

    End Class

End Namespace
